package com.dhecp.project.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.project.system.domain.SysUserAndRoleButtonState;
import com.dhecp.project.system.mapper.SysUserAndRoleButtonStateMapper;
import com.dhecp.project.system.service.SysUserAndRoleButtonStateService;

/**
 * 用户及角色功能按钮权限 服务层
 * @author DH-Xxj
 * @version 2022年11月21日
 */
@Service
public class SysUserAndRoleButtonStateServiceImpl implements SysUserAndRoleButtonStateService {
    
    @Autowired
    private SysUserAndRoleButtonStateMapper sysUserAndRoleButtonStateMapper;

	/**
	 * 查询指定用户及角色功能按钮权限
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param pid 必要参数：用户编号或角色guid
	 * @param vtype 必要参数：用户或角色类型（user：用户，role：角色）
	 * @return List - SysUserAndRoleButtonState
	 * @author DH-Xxj
	 */
    @Override
    public List<SysUserAndRoleButtonState> queryButtonState(Map<String,Object> params){
        if (params.get("pid") == null || params.get("pid") == ""){
        	params.put("pid", "pid");
        }
        if (params.get("vtype") == null || params.get("vtype") == ""){
        	params.put("vtype", "vtype");
        }
        if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
        	params.put("moduleTable", "moduleTable");
        }
    	return sysUserAndRoleButtonStateMapper.findButtonState(params);
    }

	/**
	 * 设置指定用户及角色功能按钮权限
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param pid 必要参数：用户编号或角色guid
	 * @param vtype 必要参数：用户或角色类型（user：用户，role：角色）
	 * @param infoItems 必要参数：用户及角色功能按钮权限表对应实体类的json格式字符串
	 * @return AjaxResult
	 * @author DH-Xxj
	 */
    @Override
    public AjaxResult saveButtonState(Map<String, Object> params){
        String pid = "";
        String vtype = "";
        String moduleTable = "";
        if (params.get("pid") == null || params.get("pid") == ""){
        	return AjaxResult.error(MessageUtils.message("module.error.lack","用户编号或角色guid【pid】"));
        }else{
        	pid = params.get("pid").toString();
        }
        if (params.get("vtype") == null || params.get("vtype") == ""){
        	return AjaxResult.error(MessageUtils.message("module.error.lack","用户或角色类型【vtype】"));
        }else{
        	vtype = params.get("vtype").toString();
        }
        if (params.get("moduleTable") == null || params.get("moduleTable") == ""){
        	return AjaxResult.error(MessageUtils.message("module.error.lack","功能唯一标识【moduleTable】"));
        }else{
        	moduleTable = params.get("moduleTable").toString();
        }
        sysUserAndRoleButtonStateMapper.deleteButtonState(params);
        
        List<SysUserAndRoleButtonState> sysUserAndRoleButtonStateList = JSON.parseArray(params.get("infoItems").toString(), SysUserAndRoleButtonState.class);
        if (sysUserAndRoleButtonStateList != null && sysUserAndRoleButtonStateList.size() > 0) {
            for (SysUserAndRoleButtonState sysUserAndRoleButtonState : sysUserAndRoleButtonStateList) {
                if (sysUserAndRoleButtonState != null){
                	sysUserAndRoleButtonState.setPid(pid);
                	sysUserAndRoleButtonState.setVtype(vtype);
                	sysUserAndRoleButtonState.setModuleTable(moduleTable);
                	sysUserAndRoleButtonStateMapper.insert(sysUserAndRoleButtonState);
                }
            }
        }
        
    	return AjaxResult.success("操作成功");
    }
    
}
